<?php
session_start();
header("Content-type:image/png"); //声明图片格式是png

$img_width = 100;
$img_height = 20;
srand(microtime() * 100000);

for($i=0;$i<4;$i++)
{
$new_number.=dechex(rand(0,15));
}
//将验证码输入session
$_SESSION[check_auth]=$new_number;

$number_img = imageCreate($img_width,$img_height);
imageColorallocate($number_img, 255, 255, 255);

for ($i=0;$i<strlen($_SESSION[check_auth]);$i++)
{
$font = mt_rand(3,5);
$x = mt_rand(1,8)+$img_width*$i/4;
$y = mt_rand(1,$img_height/4);
//字符颜色
$color = imageColorAllocate($number_img,mt_rand(0,100),mt_rand(0,150),mt_rand(0,200));
imageString($number_img,$font,$x,$y,$_SESSION[check_auth][$i],$color);
}
//以png格式输出
imagepng($number_img);
imagedestroy($number_img);

?>